var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

// 环境变量配置
var WEBPACK_DEV = process.env.WEBPACK_DEV || 'dev';
console.log(WEBPACK_DEV)

function getHtmlConfig (name) {
    return  {
        filename: 'view/'+ name +'.html',
        template: './src/view/'+name+'.html',
        inject: true,
        chunks: ['common', name]
    }
}
var config = {
    entry: {
        common: ["./src/page/common/index.js"],
        index: ["./src/page/index/index.js"],
        login: ["./src/page/login/login.js"]
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist',
        filename: 'js/[name].bundle.js'
    },
    externals: {
        'jquery': 'window.jQuery'
    },
    module:  {
        loaders:  [
            {
                test: /\.css$/,
                loader:  ExtractTextPlugin.extract("style-loader","css-loader")
            },
            {
                test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/,
                loader: 'url-loader?limit=100&name=resource/[name].[ext]'
            },
            {
                test: /\.string$/,
                loader: 'html-loader'
            }
        ]
    },
    plugins: [
        // 独立通用模块到 js/base.js
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',
            filename: 'js/base.js'
        }),
        // 把css单独打包到文件里
        new ExtractTextPlugin('css/[name].min.css'),
        // html 模板处理
        new HtmlWebpackPlugin(getHtmlConfig('index'))
    ]
};
if('dev' === WEBPACK_DEV) {
    config.entry.common.push( "webpack-dev-server/client?http://localhost:8080/")
}

module.exports = config;